<template>
    <div class="login-container">
        <div class="background-overlay"></div>
        <el-form :model="form" :rules="rules" ref="registerFormRef" label-width="80px" label-position="left">
            <h2>用户注册</h2>
            <el-form-item label="用&ensp;户&ensp;名" prop="username">
                <el-input v-model="form.username" />
            </el-form-item>

            <el-form-item label="密&emsp;&emsp;码" prop="password">
                <el-input v-model="form.password" type="password" show-password />
            </el-form-item>

            <el-form-item label="确认密码" prop="confirmPassword">
                <el-input v-model="form.confirmPassword" type="password" show-password />
            </el-form-item>

            <div class="button-box">
                <el-button type="primary" @click="submitRegister">注册</el-button>
                <el-button link @click="goToLogin">已有账号？立即登录</el-button>
            </div>
        </el-form>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import api from '@/services/api';
import { ElMessage } from 'element-plus';

const form = ref({ username: '', password: '', confirmPassword: '' });
const router = useRouter();

// 表单引用
const registerFormRef = ref();

// 表单验证规则
const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 15, message: '用户名长度在3到15个字符之间', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码至少6位', trigger: 'blur' }
    ],
    confirmPassword: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                if (value !== form.value.password) {
                    callback(new Error('两次输入的密码不一致'));
                } else {
                    callback();
                }
            },
            trigger: 'blur'
        }
    ]
};

async function submitRegister() {
    const valid = await registerFormRef.value.validate().catch(err => false);
    if (valid) {
        try {
            await api.register(form.value.username, form.value.password);
            ElMessage.success('注册成功，请登录');
            router.push('/login');
        } catch (error) {
            console.error('注册失败:', error);
            ElMessage.error(error.response?.data?.message || '注册失败，请重试');
        }
    } else {
        ElMessage.warning('请正确填写表单信息');
        return false;
    }
}

function goToLogin() {
    router.push('/login');
}
</script>

<style scoped>
.login-container {
    position: relative;
    max-width: 450px;
    width: 90%;
    margin: 12vh auto;
    padding: 40px 35px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 24px;
    box-shadow: 0 16px 32px rgba(0, 0, 0, 0.12);
    backdrop-filter: blur(8px);
}

h2 {
    text-align: center;
    margin-bottom: 30px;
    color: #2c3e50;
    font-weight: 650;
    font-size: 28px;
    letter-spacing: 1px;
}

.el-form :deep(.el-form-item__label) {
    font-weight: 550;
    color: #4a5568;
}

.el-button--primary {
    padding: 14px 28px;
    border-radius: 12px;
    font-size: 16px;
    letter-spacing: 0.5px;
    box-shadow: 0 4px 6px rgba(64, 158, 255, 0.15);
    width: 50%;
    margin-bottom: 10px;
}

.el-button--primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 12px rgba(64, 158, 255, 0.25);
}

.el-button--text {
    margin-top: 18px;
    font-size: 14px;
}

.button-box {
    display: flex;
    flex-direction: column;
    align-items: center;
}
</style>